package org.xw.utils;
// Copyright (c) Alibaba, Inc. and its affiliates.

import com.alibaba.dashscope.aigc.generation.Generation;
import com.alibaba.dashscope.aigc.generation.GenerationResult;
import com.alibaba.dashscope.aigc.generation.models.QwenParam;
import com.alibaba.dashscope.common.Message;
import com.alibaba.dashscope.common.Role;
import com.alibaba.dashscope.exception.ApiException;
import com.alibaba.dashscope.exception.InputRequiredException;
import com.alibaba.dashscope.exception.NoApiKeyException;

import java.util.Arrays;


public class QwenUtil {

    /**
     * 问千问问题的方法
     * @param msg
     * @return
     * @throws NoApiKeyException
     * @throws ApiException
     * @throws InputRequiredException
     */
    public static GenerationResult callWithMessage(String msg)
            throws NoApiKeyException, ApiException, InputRequiredException {
        try {
            Generation gen = new Generation();
            Message userMsg = Message.builder().role(Role.USER.getValue()).content(msg).build();
            QwenParam param =
                    QwenParam.builder()
                            .apiKey("sk-0e0ca4e4b6a445d4880f106d1e684688")
                            .model("qwen2-57b-a14b-instruct").messages(Arrays.asList(userMsg))
                            .resultFormat(QwenParam.ResultFormat.MESSAGE)
                            .topP(0.8)
                            .build();
            GenerationResult result = gen.call(param);
            System.out.println(result);
            return result;
        } catch (ApiException | NoApiKeyException | InputRequiredException e) {
            System.out.println(e.getMessage());
        }
        System.exit(0);
        return null;
    }
}


//    public static void main(String[] args){
//        try {
//            callWithMessage();
//        } catch (ApiException | NoApiKeyException | InputRequiredException e) {
//            System.out.println(e.getMessage());
//        }
//        System.exit(0);
//    }
//}